﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace System.Data.Common
{
    //DbProviderFactory is sealed. But it will take extension methods. Which is good enough.
    //
    // Provides overloads to basic constructors which currently are parameterless
    public static class DbProviderFactoryExtensions
    {
        

        public static DbConnection CreateConnection(this DbProviderFactory provider, string connectionString)
        {
            DbConnection con = provider.CreateConnection();
            if (con == null)
            {
                throw new InvalidOperationException("Failed to get a connection from the provider");
            }
            con.ConnectionString = connectionString;

            return con;
        }

        public static DbCommand CreateCommand(this DbProviderFactory provider, string text)
        {
            DbCommand com = provider.CreateCommand();
            if (com == null)
            {
                throw new InvalidOperationException(
                    "provider.CreateCommand() returned null. Does the current provider support ADO commands?");
            }
            com.CommandText = text;

            return com;
        }


        public static DbParameter CreateParameter(this DbProviderFactory provider, string name, DbType type)
        {
            DbParameter parameter = provider.CreateParameter();
            if (parameter == null)
            {
                throw new InvalidOperationException(
                    "provider.CreateParameter() returned null. Does the current provider support ADO parameters?");
            }
            parameter.ParameterName = name;
            parameter.DbType = type;
            return parameter;
        }
    }
}
